﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Avatar.Model;
using AvatarHelper;

public partial class Management_Location : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            using (var _adc = DBHelper.GetDataContext())
            {
                var _baoding = _adc.Area.Where(a => a.Name == "保定").FirstOrDefault();
                if (_baoding != null)
                {
                    gv_Location.DataSource = _adc.Area.Where(a => a.FatherID == _baoding.ID).OrderBy(a => a.Order);
                    gv_Location.DataBind();

                }
            }
            btn_addnew.Visible = true;

            panel_AddNew.Visible = false;
            panel_Update.Visible = false;
        }
    }
    protected void btn_Select_Click(object sender, EventArgs e)
    {
        using (var _adc = DBHelper.GetDataContext())
        {
            var _btn = sender as Button;
            var _id = string.IsNullOrEmpty(_btn.CommandArgument) ? 0 : Convert.ToInt32(_btn.CommandArgument);
            var _area = _adc.Area.Where(a => a.ID == _id).FirstOrDefault();
            if (_area != null)
            {
                txt_update_AreaName.Text = _area.Name;
                txt_ID.Value = _area.ID.ToString();
                txt_Order.Text = _area.Order.ToString();

                btn_addnew.Visible = false;
                panel_AddNew.Visible = false;
                panel_Update.Visible = true;
            }
        }
    }
    protected void btn_Delete_Click(object sender, EventArgs e)
    {
        try
        {
            var _btn = sender as Button;
            int _id = string.IsNullOrEmpty(_btn.CommandArgument) ? 0 : Convert.ToInt32(_btn.CommandArgument);
            using (var _adc = DBHelper.GetDataContext())
            {
                var _area = _adc.Area.Where(a => a.ID == _id).FirstOrDefault();
                if (_adc == null) throw new Exception("地区不存在");
                    _adc.Area.DeleteOnSubmit(_area);

                _adc.SubmitChanges();
            }
        }
        catch (Exception _e)
        {
            throw new Exception("删除地区出错:"+_e.Message);
        }
    }
    protected void btn_Update_Click(object sender, EventArgs e)
    {
        try
        {
            if (string.IsNullOrEmpty(txt_update_AreaName.Text)) throw new Exception("地区名称不能为空");
            var _id = string.IsNullOrEmpty(txt_ID.Value) ? 0 : Convert.ToInt32(txt_ID.Value);
            using (var _adc = DBHelper.GetDataContext())
            {
                var _area = _adc.Area.Where(a => a.ID == _id).FirstOrDefault();
                if (_area != null)
                {
                    int _order=string.IsNullOrEmpty(txt_Order.Text) ? 0 : Convert.ToInt32(txt_Order.Text);
                    if (_order == 0) throw new Exception("排序编号不能为0");

                    _area.Name = txt_update_AreaName.Text;
                    _area.Order = _order;
                }
                _adc.SubmitChanges();
            }

            Response.Redirect(Request.Url.ToString());
        }
        catch (Exception _e)
        {
            lbl_Error.Text = "更新地区出错:" + _e.Message;
        }

    }
    protected void btn_Add_Click(object sender, EventArgs e)
    {
        try
        {
            if (string.IsNullOrEmpty(txt_add_AreaName.Text)) throw new Exception("地区名称不能为空");
            using (var _adc = DBHelper.GetDataContext())
            {
                var _baoding = _adc.Area.Where(a => a.Name == "保定").FirstOrDefault();
                Area _area = new Area();
                _area.Name = txt_add_AreaName.Text;
                _area.FatherID = _baoding.ID;
                _area.Order = _adc.Area.Max(a => a.Order) + 10;

                _adc.Area.InsertOnSubmit(_area);
                _adc.SubmitChanges();
            }
            Response.Redirect(Request.Url.ToString());
        }
        catch (Exception _e)
        {
            lbl_Error.Text = "添加地区出错:" + _e.Message;
        }
    }
    protected void btn_addnew_Click(object sender, EventArgs e)
    {
        btn_addnew.Visible = false;

        panel_AddNew.Visible = true;
        panel_Update.Visible = false;
    }
    protected void btn_Cancel_Click(object sender, EventArgs e)
    {
        Response.Redirect(Request.Url.ToString());
    }
}
